<?php

/**
 * @author dungdhs@gmail.com
 * @copyright 2012
 */

function arg($id=null, $default=null){
  global $dzUrl;
  static $args = null;
  
  if (is_null($args)){
    $q = PGRequest::getVar('q', '');
    $q = empty($q)?HOMEPAGE_URL:$q;
    
    if (isset($dzUrl[$q])) cheader($dzUrl[$q]);
    
    $q = url_get_path($q);
    $args = explode('/', $q);
  }
  
  if (is_null($id)) return $args;
    
  return (isset($args[$id])&&!empty($args[$id]))?$args[$id]:$default;
}

function l($url, $absolute=false, $aQuery=array()){
  global $dzUrl;
  
  foreach ($aQuery as $id=>$query) $aQuery[$id] = sprintf('%s=%s', $id, $query);
  $sQuery = implode('&', $aQuery);
  
  $url = ($url=='<front>')?'':$url;
  $return = "";
  
  if (file_exists(PG_ROOT.$url)) $return = ($absolute)?PG_URL_ABSOLUTE.$url:PG_URL.$url;
  elseif (isset($dzUrl[$url])) $return = ($absolute)?PG_URL_ABSOLUTE.$dzUrl[$url]:PG_URL.$dzUrl[$url];
  else $return = ($absolute)?PG_URL_ABSOLUTE.$url:PG_URL.$url;
  
  if (!empty($sQuery)){
    $return .= (strpos($return, '?')===false)?'?'.$sQuery:'&'.$sQuery;
  }
  $return = str_replace(' ', '%20', $return);
  return $return;
}

function url_load(){
  global $database, $dzUrl, $dzUrlAlias;
  
  $dzUrl = $dzUrlAlias = array();
  $res = $database->db_query("SELECT * FROM urls ORDER BY url_id ASC");
  while ($row = $database->db_fetch_assoc($res)){
    $dzUrlAlias[$row['url_alias']]= $row['url_path'];
    $dzUrl[$row['url_path']]= $row['url_alias'];
  }
}

function url_get_path($url){
  global $dzUrlAlias;
  
  return isset($dzUrlAlias[$url])?$dzUrlAlias[$url]:$url;
}

function url_set_alias($url, $alias){
  global $dzUrl, $dzUrlAlias, $database;
  
  $newAlias = $alias = make_friendly_title($alias);
  $i = 1;
  while (isset($dzUrlAlias[$newAlias]) && ($dzUrlAlias[$newAlias]!=$url)){
    $newAlias = $alias.'-'.$i; $i++;
  }
  $alias = $newAlias;
  
  if (isset($dzUrl[$url])){
    unset($dzUrlAlias[$dzUrl[$url]]);
    
    $database->db_query("UPDATE urls SET url_alias='%s' WHERE url_path='%s'", $alias, $url);
  }else{
    $database->db_query("INSERT INTO urls(url_path, url_alias) VALUES ('%s', '%s')", $url, $alias);
  }
  
  $dzUrl[$url] = $alias;
  $dzUrlAlias[$alias] = $url;
}

function is_front($setFront=false){
	static $isFront = false;
	if ($setFront) $isFront=true;
	return $isFront;
}

function url_execute(){
  global $dzTask, $crURL;
  
  $module_name = arg(0, 'homepage');
  $task_name = $module_name.'/'.arg(1, 'default');
  
  $crURL = $task_name;
  
  if ($task_name==HOMEPAGE_URL){ 
  	theme_body_class('page-front');
		is_front(true);
	}
  theme_body_class('page-'.$module_name.'-'.arg(1, 'default'));
  if ($id=arg(2, false)) theme_body_class('page-'.$module_name.'-'.arg(1, 'default').'-'.$id);
  
  if (isset($dzTask[$task_name])){
    $task = $dzTask[$task_name];
    
    if (check_perm($task['task_perm']) && function_exists($task['task_func'])){
      if (!empty($task['task_title'])){ 
        theme_title($task['task_title']);
        __ogp_set('og:title', $task['task_title']);
      }
      
      require_once(MODULE_PATH.$module_name.'/'.$module_name.'.php');
      return call_user_func($task['task_func']);
    }  
  }
  
  cheader('<front>');
}

function make_friendly_title($text){
  $text = trim($text);
  //Charachters must be in ASCII and certain ones aint allowed
  $text = html_entity_decode ($text);
  $text = preg_replace("/(ä|à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $text);
  $text = str_replace("ç","c",$text);
  $text = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $text);
  $text = preg_replace("/(ì|í|î|ị|ỉ|ĩ)/", 'i', $text);
  $text = preg_replace("/(ö|ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $text);
  $text = preg_replace("/(ü|ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $text);
  $text = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $text);
  $text = preg_replace("/(đ)/", 'd', $text);
  //CHU HOA
  $text = preg_replace("/(Ä|À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $text);
  $text = str_replace("Ç","C",$text);
  $text = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $text);
  $text = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $text);
  $text = preg_replace("/(Ö|Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $text);
  $text = preg_replace("/(Ü|Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $text);
  $text = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $text);
  $text = preg_replace("/(Đ)/", 'D', $text);
  $text = str_replace(" ","-",$text);
  
  $text = preg_replace("/[^A-Za-z0-9-\/]/", "", $text);
  
  $text = str_replace("----","-",$text);
  $text = str_replace("---","-",$text);
  $text = str_replace("--","-",$text);
  
  $text = trim($text, '-');
  $text = strtolower($text);
  
  return $text;
}

function isCurrentUrl($url){
  global $crURL;
  
  return $crURL==$url;
}

?>